package com.binance.rabbitmq.processor;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;

import java.util.UUID;

/**
 * Created by robin.wu on 2018/11/29.
 */
public class DefaultCorrelationPostProcessor implements CorrelationPostProcessor {

    @Override
    public Message postProcessMessage(Message message) throws AmqpException {
        MessageProperties messageProperties = message.getMessageProperties();
        String correlationId = messageProperties.getCorrelationIdString();
        if (correlationId == null) {
            correlationId = UUID.randomUUID().toString();
            messageProperties.setCorrelationIdString(correlationId);
        }
        messageProperties.getHeaders().put("correlation-id", correlationId);
        return message;
    }
}
